﻿/**
 * Created by arvin on 2017/3/28.
 */
var Bearcat = require('bearcat');
var Async = require("async")

module.exports = function () {
    return Bearcat.getBean({
        id: "monitor",
        func: monitor,
        props: [
            {name: "app", "ref": "application"}
        ]
    });
}

function monitor() {
    this.app = null;
}

var CheckMotionDevice = function (msg) {
    if(msg.params.motion == undefined)
        return false;

    return true;
}

monitor.prototype.handle = function (msg,next) {
     
    if(!CheckMotionDevice(msg))
    {
        next(null,{code:101,msg:101});
        return;
    }

    var model = this.app.getComponent("dao").getConnection().model("MotionDevices");
    var wechatData = this.app.getComponent("dao").getConnection().model("wechatMsg");
    Async.waterfall(
        [
            function (cb) { // 查询设备ID是否可用
                model.invalidate(msg.params.motion,function (invaild) {
                    if(!invaild)
                    {
                        next(null,{code:101,msg:"设备ID有问题"});
                        return;
                    }
                    cb();
                })
            },
            function (cb) { // 如果这个设备可用，查询这个设备的状态
                model.GetMotionState(msg.params.motion,function (motionState) {
                    if(motionState == 1)
                    {
                        next(null,{code:103,msg:"这个设备正在使用"});
                        return;
                    }
                    else if(  motionState == 99 )
                    {
                        next(null,{code:2,msg:"这个设备正在维修"});
                        return;
                    }

                    cb();
                });
            },
            function () { // 如果这个设备是空闲的，去商品列表里查询,并且是未使用状态,告诉客户端本条数据
                 wechatData.SearchTarDevice({MotionDevice:msg.params.motion, IsUsed:false },function (result){
                    if(result !=null) {

                        wechatData.UpdateUsedData({_id:result._id});
                        model.UpdateMotionState({MotionDevice: msg.params.motion, MotionState: 1}, function (suc) {
                            if (suc) {
                                console.warn("发送使用数据：机器名：%s， 设备ID:%s" , result.MotionName,result.MotionDevice);
                                next(null, {
                                    code: 1, msg: {
                                        MotionDevice: result.MotionDevice,// 二维码自带数据
                                        MotionName: result.MotionName,// 二维码自带数据(如果都是Adm，就没有意义)
                                        WechatName: result.WechatName,// 丁哲提供
                                        TimeLength: result.TimeLength,// 时间长度 单位是分钟丁哲提供
                                        TurnLength: result.TurnLength,// 圈数，暂时不考虑
                                    }
                                });
                            }
                            else
                                next(null, {code: 106, msg: "更新设备信息有问题"});
                        });

                        // 这里是防止出错。两边数据严格一直都是分钟
                        //var oneSecond = 1000 * result.TimeLength * 10; // one min  = 1000 x 1 ms * 60
			            var oneSecond = 1000 * result.TimeLength ; // one min  = 1000 x 1 ms * 60
                        setTimeout(function() {
                            model.UpdateMotionState({MotionDevice: msg.params.motion, MotionState: 0},function (suc) {
                                if (suc) {
                                    console.log("服务器时间刷会机器");
                                }
                            });
                        }, oneSecond);
                    }
                    else
                    {
                        next(null,{code:104,msg:"等待激活"});
                    }

                });
            }
        ]
    );

}